package com.sinven.serivce;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinven.common.Pager;
import com.sinven.mapper.UserMapper;
import com.sinven.pojo.Content;
import com.sinven.pojo.User;

@Service
public class UserSerivceImpl implements UserSerivce {

	@Autowired
	private UserMapper userMapper;

	@Override
	// 登录
	public User queryByUserCode(String userCode, String password) {
		try {
			User user = userMapper.queryByUserCode(userCode);// 调用queryByUserCode的方法查询用户名
			// 判断密码是否相同
			if (!user.getPassword().equals(password)) {
				return null;
			}
			return user;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	// 修改
	public Boolean updateUser(User user) {
		int rows = userMapper.updateUser(user);// 根据updateUsuser方法修改当前行
		return rows > 0 ? true : false;// 如果rows大于0，返回true，否则返回false
		// TODO Auto-generated method stub

	}

	@Override
	public Boolean queryByUserName(String userName) {
		int rows = userMapper.queryByUserName(userName);
		return rows > 0 ? true : false;// 如果rows大于0，返回true，否则返回false
	}

	@Override
	public Boolean queryByUserCodez(String userCode) {
		int rows = userMapper.queryByUserCodez(userCode);
		return rows > 0 ? true : false;// 如果rows大于0，返回true，否则返回false
	}

	@Override
	public Boolean addUser(User user) {
		int rows=userMapper.addUser(user);
		return rows > 0 ? true : false;// 如果rows大于0，返回true，否则返回false
	}

	@Override
	public Pager<List<User>> queryAll(Long startIndex,Integer pageSize) {
		 long totalRecords = userMapper.countUser();
		 if (totalRecords == 0) {
				return null;
			}
		 Pager<List<User>> pager = 
				 new Pager<>(totalRecords, startIndex, pageSize);
		 List<User> contentlist = userMapper.queryAll(pager.getStartIndex(), pager.getPageSize());
		 pager.setData(contentlist);
		 return pager;
	}
}
